const data = Array.from(Array(2), (v, i) => {
  return {
    account: "uaa" + i + 1,
    level: Math.ceil(Math.random() * 10),
    id: new Date().valueOf() + i
  }
})

/**
 * @description 获取数据
 * @param {Object} params 
 * @param {String} params.account 账号名 
 * @returns {Array<Object>}
 */
export function getDataApi(params = {}) {
  const { account } = params
  if (!account) {
    return [...data]
  } else {
    return [...data.filter(item => item.account.includes(account))]
  }
}
/**
 * @description 新增一个账号
 * @param {Object} params 
 * @param {String} params.account 账号名
 * @param {String} params.level 账号等级
 * @returns {undefined} 无返回
 */
export function addAccountApi(params) {
  const { account, level } = params
  data.push({ account, level, id: new Date().valueOf() })
}

/**
 * @description 删除账号
 * @param {Object} params 
 * @param {String} params.account 账号名
 * @param {String} params.level 账号等级
 * @returns {undefined} 无返回
 */
export function deleteAccountApi(params) {
  const { id } = params
  let delIndex = -1
  data.forEach((item, index) => {
    if (item.id === id) {
      delIndex = index
    }
  })
  if (delIndex !== -1) {
    data.splice(delIndex, 1)
  }
}
/**
 * @description 更新账号
 * @param {Object} params 
 * @param {String} params.account 账号名
 * @param {String} params.level 账号等级
 * @param {String} params.id 账号id
 * @returns {undefined} 无返回
 */
export function updateAccountApi(params) {
  const { account, level, id } = params
  let updateIndex = -1
  data.forEach((item, index) => {
    if (item.id === id) {
      updateIndex = index
    }
  })
  if (updateIndex !== -1) {
    data.splice(updateIndex, 1, { account, level, id })
  }
}